package com.doform.android.xianggucheng.activity.login;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.activity.BaseTitleActivity;
import com.doform.android.xianggucheng.bean.request_bean.ReqRegister;
import com.doform.android.xianggucheng.bean.request_bean.ReqRegister2;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.utils.Constans;
import com.doform.android.xianggucheng.utils.TsUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2017-07-27.
 * GitHub：https://github.com/cxydxpx
 */

public class RegisterActivity2 extends BaseTitleActivity {

    private TimeCount time;

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            //参数依次为总时长,和计时的时间间隔
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            //计时完毕时触发
            btn_post.setText("重新获取");
            btn_post.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            //计时过程显示
            btn_post.setClickable(false);
            btn_post.setText(millisUntilFinished / 1000 + "秒");
        }
    }


    @BindView(R.id.et_code)
    EditText et_code;

    @BindView(R.id.tv_message)
    TextView tv_message;


    @BindView(R.id.btn_post)
    Button btn_post;

    private String mPhone;

    @Override
    protected int layoutResId() {
        return R.layout.activity_register2;
    }

    @Override
    protected void init() {
        super.init();

        time = new TimeCount(60000, 1000);

        String stringExtra = getIntent().getStringExtra(Constans.EXTRA_REGISTER_PHONE);
        this.mPhone = stringExtra;

        if (mPhone != null) {
            //获取到验证码时
            time.start();
            tv_message.setText("验证码已发至手机号" + mPhone);
        }

    }

    @Override
    protected void showView() {
        titleBar.setTextTitle("注册");

    }

    private void beforeNext() {

        String code = et_code.getText().toString().trim();
        if (TextUtils.isEmpty(code)) {
            TsUtils.showToast("请输入验证码");
            return;
        }
        checkCode(code);
    }

    /**
     * 校验验证码
     *
     * @param code
     */
    private void checkCode(String code) {
        HttpManager.instance()
                .postCodeToService2(new ReqRegister2(mPhone, code))
                .subscribe(new ErrorHandleSubscriber<Integer>() {
                    @Override
                    public void onNext(@NonNull Integer Integer) {
                        startActivity(new Intent(RegisterActivity2.this, RegisterActivity3.class)
                        .putExtra(Constans.EXTRA_REGISTER_PHONE2,mPhone));
                        finish();
                    }
                });
    }

    /**
     * 发送手机号 获取验证码
     */
    private void beforePostCode() {

        if (mPhone != null) {
            HttpManager.instance()
                    .postCodeToService(new ReqRegister(mPhone))
                    .subscribe(new ErrorHandleSubscriber<String>() {
                        @Override
                        public void onNext(@NonNull String str) {
                            tv_message.setText("验证码已发至手机号" + mPhone);
                            //获取到验证码时
                            time.start();
                        }
                    });
        } else {
            TsUtils.showToast("系统错误，请重新注册");
        }


    }

    @OnClick({R.id.btn_next, R.id.btn_post})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
                beforeNext();
                break;
            case R.id.btn_post:
                beforePostCode();
                break;
            default:
                break;
        }
    }


}
